﻿//////////////////////////////////////////////////////////////////
//
// ITransport.cs
//
// Copyright (c) 2012 Dan Pike.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation files
// (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Interfaces
{
   ////////////////////////////////////////////////////////////////////////
   /// <summary>
   /// Arguments to the NetworkStateChanged event
   /// </summary>
   public enum NetworkStateConstants
   {
      None,
      Idle,          // Waiting to create network connection
      Connected,     // Network connection established to server
   }
   public class NetworkStateChangedEventArgs : EventArgs
   {
      public NetworkStateConstants state;
   }
   public delegate void NetworkStateChangedHandler(object sender, NetworkStateChangedEventArgs eventArgs);

   public class ReceivedMessageEventArgs : EventArgs
   {
      public string service;
      public string mnemonic;
      public IMessage message;
   }
   public delegate void ReceivedMessageHandler(object sender, ReceivedMessageEventArgs eventArgs);
      
   /// <summary>
   /// All communication goes through a network object which may do
   /// an internal loop-back, or use TCP or whatever is appropriate
   /// </summary>
   public interface ITransport
   {
      /// <summary>
      /// Start up the network
      /// </summary>
      /// <returns></returns>
      bool Initialize(string senderId);

      /// <summary>
      /// Shut down the network
      /// </summary>
      /// <returns></returns>
      void Shutdown();

      /// <summary>
      /// Triggered when the network changes state
      /// </summary>
      event NetworkStateChangedHandler EventStateChanged;

      /// <summary>
      /// Triggered when a message arrives over the network
      /// </summary>
      event ReceivedMessageHandler EventReceivedMessage;

      /// <summary>
      /// Connect a user to the network (will login after this)
      /// </summary>
      /// <param name="account"></param>
      /// <returns></returns>
      bool Connect(IAccount account);

      /// <summary>
      /// Disconnect a user from the network (will already have logged out)
      /// </summary>
      /// <param name="account"></param>
      /// <returns></returns>
      bool Disconnect(IAccount account);

      /// <summary>
      /// Broadcast a message to everyone. Need to pass the interface that
      /// defines the message type (e.g. IHeartbeat) in the generic parameter T
      /// because that will be passed in the message to the recipient, so it,
      /// in turn, will know how to interpret the message body
      /// </summary>
      /// <param name="message"></param>
      /// <returns></returns>
      bool Broadcast<T>(IMessage message) where T : IMessage;

      /// <summary>
      /// We are a client and we want to start receiving notifications about a planet
      /// </summary>
      /// <param name="planet">name of the planet</param>
      /// <returns>true if the request was processed</returns>
      bool OpenPlanet(string planet);

      /// <summary>
      /// We are a client and we are no longer interested in notifications about a planet
      /// </summary>
      /// <param name="planet">name of the planet</param>
      /// <returns>true if the request was processed</returns>
      bool ClosePlanet(string planet);

      /// <summary>
      /// We are a server and we want to announce that a planet is running
      /// </summary>
      /// <param name="planet">name of the planet</param>
      /// <returns>true if the request was processed</returns>
      bool StartPlanet(string planet);

      /// <summary>
      /// We are a server and we want to announce that a planet has stopped running
      /// </summary>
      /// <param name="planet">name of the planet</param>
      bool StopPlanet(string planet);
      
      /// <summary>
      /// Request a tile from the server, it's not on our cache
      /// </summary>
      bool LoadTile(string planetName, ILocation location);

      /// <summary>
      /// An image (contains all the data) for a tile
      /// </summary>
      bool TileImage(string planetName, ILocation location, short[,] heights);
   }
}
